﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;

namespace FundRaising
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {

        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

            if (HttpContext.Current.User != null)
            {
                // see if this user is authenticated, any authenticated cookie (ticket) exists for this user
                if (HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    // see if the authentication is done using FormsAuthentication
                    if (HttpContext.Current.User.Identity is FormsIdentity)
                    {
                        // Get the roles stored for this request from the ticket
                        // get the identity of the user
                        FormsIdentity identity = (FormsIdentity)HttpContext.Current.User.Identity;
                        // get the forms authetication ticket of the user
                        FormsAuthenticationTicket ticket = identity.Ticket;
                        // get the roles stored as UserData into the ticket
                        string[] roles = getRole(ticket.Name);
                        HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(identity, roles);
                    }
                }
            }

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }

         private string[] getRole(string UserName)
        {

            string[] role = new string[1];
            if (UserName.Equals("god"))
            {
                role[0] = "admin";
                return role;
            }
            try
            {
                FundRaising.BLL.Login userBLL = new FundRaising.BLL.Login();
                if (userBLL.GetDataByLogName(UserName).Count < 1)
                    return null;
                FundRaising.DAL._FR_DS.LoginRow loginUserRow = userBLL.GetDataByLogName(UserName)[0];
                
                role[0] = "user";
                return role;
            }
            //TODO dispose here
            finally
            {

            }

        }


        private bool IsAdministrator(string UserType)
        {
            FundRaising.BLL.Login userBLL = new FundRaising.BLL.Login();
            string UserTypeNum = userBLL.GetDataByLogName("Admin")[0].UserName;
            if (UserTypeNum == UserType) return true;

            return false;

        }
    
    }
}